package com.jcz.rent.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jcz.entity.House;
import com.jcz.entity.HouseCondition;
import com.jcz.entity.Page;
import com.jcz.rentBiz.HouseBiz;
import com.jcz.rentBizImpl.HouseBizImpl;

/**
 * Servlet implementation class HouseServlet
 */
@WebServlet("/HouseServlet")
public class HouseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HouseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("opr");
		HouseBiz biz = new HouseBizImpl();
		HttpSession session = request.getSession();

		String pageIndex = request.getParameter("pageIndex");

		Integer index = 1;
		Integer pageSize = 3;
		if (pageIndex != null) {
			index = Integer.parseInt(pageIndex);
		}

		if ("list".equals(action)) {
			List<House> hs = biz.showHouseByCondition(null, index, pageSize);

			Page<House> page = new Page<House>();
			page.setPageIndex(index);
			page.setPageSize(pageSize);
			page.setCount(biz.getHouseCount(null));
			page.setList(hs);

			session.setAttribute("page", page);
			// response.sendRedirect("page/list.jsp");
			request.getRequestDispatcher("page/list.jsp").forward(request,
					response);
		} else if ("find".equals(action)) {
			String title = request.getParameter("title");
			String price = request.getParameter("price");
			String floorage = request.getParameter("floorage");
			String street_id = request.getParameter("street_id");
			String type_id = request.getParameter("type_id");

			HouseCondition c = new HouseCondition();

			// ����
			if (title != null && !"".equals(title)) {
				c.setTitle("%" + title + "%");
			}

			if (street_id != null && !"".equals(street_id)) {
				c.setStreetId(Short.valueOf(street_id));
			}
			if (type_id != null && !"".equals(type_id)) {
				c.setTypeId(Short.valueOf(type_id));
			}

			// �۸�
			if (price != null && !"".equals(price)) {
				Integer minPrice = Integer.parseInt(price.substring(0,
						price.indexOf("-")));// ��ͼ۸�
				Integer maxPrice = Integer.parseInt(price.substring(price
						.indexOf("-") + 1));
				c.setMinPrice(minPrice);
				c.setMaxPrice(maxPrice);
			}

			if (floorage != null && !"".equals(floorage)) {
				Short minFloorage = Short.parseShort(floorage.substring(0,
						floorage.indexOf("-")));
				Short maxFloorage = Short.parseShort(floorage
						.substring(floorage.indexOf("-") + 1));
				c.setMinFloorage(minFloorage);
				c.setMaxFloorage(maxFloorage);
			}

			List<House> hs = biz.showHouseByCondition(c, index, pageSize);

			Page<House> page = new Page<House>();
			page.setPageIndex(index);
			page.setPageSize(pageSize);
			page.setCount(biz.getHouseCount(c));
			page.setList(hs);

			session.setAttribute("page", page);
			// response.sendRedirect("page/list.jsp");
			request.getRequestDispatcher("page/list.jsp").forward(request,
					response);

		}
	}

}
